﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using Warehouse.Models;

namespace Warehouse.Controllers
{ 
    public class TypeAccountController : Controller
    {
        private readonly WarehouseEntities _db = new WarehouseEntities();

        //
        // GET: /TypeAccount/

        public ViewResult Index()
        {
            return View(_db.TypeAccount.ToList());
        }

        public ViewResult Details(int id)
        {
            TypeAccount typeaccount = _db.TypeAccount.Single(t => t.ID == id);
            return View(typeaccount);
        }

        public ActionResult Create()
        {
            return View();
        } 

        [HttpPost]
        public ActionResult Create(TypeAccount typeaccount)
        {
            if (ModelState.IsValid)
            {
                _db.TypeAccount.AddObject(typeaccount);
                _db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(typeaccount);
        }

        public ActionResult Edit(int id)
        {
            TypeAccount typeaccount = _db.TypeAccount.Single(t => t.ID == id);
            return View(typeaccount);
        }

        [HttpPost]
        public ActionResult Edit(TypeAccount typeaccount)
        {
            if (ModelState.IsValid)
            {
                _db.TypeAccount.Attach(typeaccount);
                _db.ObjectStateManager.ChangeObjectState(typeaccount, EntityState.Modified);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(typeaccount);
        }

        public ActionResult Delete(int id)
        {
            TypeAccount typeaccount = _db.TypeAccount.Single(t => t.ID == id);
            return View(typeaccount);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            TypeAccount typeaccount = _db.TypeAccount.Single(t => t.ID == id);
            _db.TypeAccount.DeleteObject(typeaccount);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}